<?php


namespace app\zc\controller;


use app\zc\model\NavModel;
use app\zc\validata\NavValidata;
use cmf\controller\AdminBaseController;

class NavController extends AdminBaseController
{

    public function index()
    {
        //搜索
        $keyword = input('keyword/s','');
        $where = [];
        if($keyword){
            $where[] = ['title|author','like','%'.$keyword.'%'];
        }

        //导航列表显示
        $list = NavModel::order(['list_order'=>'DESC','id'=>'DESC'])
            ->where($where)
            ->paginate(5);
        $this->assign('list',$list);

        return $this->fetch('nav');
    }

    public function px(){
        $nav = new NavModel();
        parent::listOrders($nav);
        $this->success('排序成功');
    }

    public function add(){
        //添加页面显示
        return $this->fetch();
    }

    public function addPost(){
        $data = input('post.');

        //验证器验证
        $validata = new NavValidata();
        $result = $validata->scene('add')->check($data);
        if(!$result){
            $this->error($validata->getError());
        }

        //添加逻辑
        $add = NavModel::create($data);

        if($add){
            $this->success('添加成功','News/index');
        } else {
            $this->error('添加失败');
        }
    }

    public function edit(){
        //接收id
        $id = input('id','');
        //编辑页面显示与数据还原
        $list = NavModel::find($id);

        $this->assign('list',$list);
        return $this->fetch();

    }

    public function editPost(){
        //接收编辑数据
        $data = input('post.');

        //验证器验证
        $validata = new NavValidata();
        $result = $validata->scene('add')->check($data);
        if(!$result){
            $this->error($validata->getError());
        }

        //编辑数据逻辑
        $edit = NavModel::find($data['id']);
        if(!$edit){
            $this->error('未查询到数据');
        }
        $edit->title = $data['title'];
        $edit->url = $data['url'];
        $edit->parent_id = $data['parent_id'];
        if($edit->save()){
            $this->success('保存成功','Nav/index');
        }else{
            $this->error('保存失败');
        }
    }

    public function delete(){
        //接收删除数据id
        $id = input('id','');
        //删除逻辑
        if(NavModel::destroy($id)){
            $this->success('删除成功','nav/index');
        }else{
                $this->error('删除失败');
        }

    }

}